const whiteList = ["/admin/user/login"];

const { timestamp } = require("../utils/tools");

module.exports = () => {
  return async (ctx, next) => {
    let token = ctx.request.header.token;
    if (!whiteList.some((item) => item === ctx.request.url)) {
      const { last_time } = await ctx.app.mysql.get("login_user", {
        token,
      });
      const current_time = timestamp();
      if (current_time - last_time > 30*60*1000) {
        const result = await ctx.app.mysql.delete("login_user", {
          token,
        });
        ctx.body = {
          data: {},
          code: 302,
          message: "token失效",
        };
      } else {
        await next();
      }
    } else {
      await next();
    }
  };
};
